package com.hyt.it.ogt.ks.util;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * @author 刘恒活
 * @since 2020/11/20 10:50
 */
public class StringUtil {
    public final static String REGEX_START = "^[　*| *| *|//s*]*";
    public final static String REGEX_END = "[　*| *| *|//s*]*$";
    public final static String REPLACE = "";
    public final static String REGEX = "[^(0-9)]";

    public static void main(String[] args) throws URISyntaxException {

        System.out.println(StringUtils.isBlank(""));
    }

    /**
     * 加密字符串
     *
     * @param str
     */
    public static String encrypt(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        } else {
            Pattern pattern = Pattern.compile(REGEX);
            str = pattern.matcher(str).replaceAll("").trim();
            char[] chars = str.toLowerCase().toCharArray();
            StringBuffer sb = new StringBuffer();
            for (char c : chars) {
                sb.append(replace(c));
            }
            return sb.toString();
        }
    }

    private static char replace(char c) {
        if (c == '0') {
            return '4';
        } else if (c == '1') {
            return '8';
        } else if (c == '2') {
            return '6';
        } else if (c == '3') {
            return '1';
        } else if (c == '4') {
            return '0';
        } else if (c == '5') {
            return '7';
        } else if (c == '6') {
            return '9';
        } else if (c == '7') {
            return '3';
        } else if (c == '8') {
            return '2';
        } else if (c == '9') {
            return '5';
        } else if (c == 'k') {
            return 'p';
        } else if (c == 'b') {
            return 'k';
        }
        return c;
    }

    public static String decrypt(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        } else {
            char[] chars = str.toLowerCase().toCharArray();
            StringBuffer sb = new StringBuffer();
            for (char c : chars) {
                sb.append(reverse(c));
            }
            return sb.toString();
        }
    }

    private static char reverse(char c) {
        if (c == '4') {
            return '0';
        } else if (c == '8') {
            return '1';
        } else if (c == '6') {
            return '2';
        } else if (c == '1') {
            return '3';
        } else if (c == '0') {
            return '4';
        } else if (c == '7') {
            return '5';
        } else if (c == '9') {
            return '6';
        } else if (c == '3') {
            return '7';
        } else if (c == '2') {
            return '8';
        } else if (c == '5') {
            return '9';
        } else if (c == 'p') {
            return 'k';
        } else if (c == 'k') {
            return 'b';
        }
        return c;
    }

    /**
     * 去除字符串中 头部 和 尾部 所包含的空格
     * 包括:空格(全角，半角)、制表符、换页符等
     *
     * @param s
     * @return
     */
    public static String trims(String s) {
        if (s == null) {
            return null;
        }
        return s.replaceAll(REGEX_START, REPLACE).replaceAll(REGEX_END, REPLACE);
    }
}
